package rn;

import java.util.Collection;
import java.util.ResourceBundle;
import java.util.StringTokenizer;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;

/**
 *
 * @author Roberta
 */
public class Email {

    public static String assunto;
    public static String destino;
    public static String conteudo;
    public static String nome;
    public static Collection emails;
    private String emailFrom;
    private String emailFromN;
    private String autentcEmail;
    private String autentcSenha;
    private String host;
    private int porta;

    public Email() {
        ResourceBundle p = ResourceBundle.getBundle("rn.emailDados");
        host = p.getString("HostName");
        emailFrom = p.getString("From");
        emailFromN = p.getString("FromName");
        autentcEmail = p.getString("Authentication");
        autentcSenha = p.getString("Senha");
        porta = Integer.parseInt(p.getString("Port"));
    }

    public void enviaEmail() throws EmailException {
        try {
            StringTokenizer stPara = new StringTokenizer(destino, ",");
            while (stPara.hasMoreTokens()) {
                SimpleEmail email = new SimpleEmail();
                email.setHostName(host); // o servidor SMTP para envio do e-mail  
                email.addTo(stPara.nextToken().trim()); //destinatário  
                email.setFrom(emailFrom, emailFromN); // remetente  
                email.setSubject(assunto + "."); // assunto do e-mail  
                email.setMsg(conteudo); //conteudo do e-mail  
                email.setAuthentication(autentcEmail, autentcSenha);
                email.setSmtpPort(porta);
                email.setSSL(true);
                email.setTLS(true);
                email.send();
            }
            }  catch (EmailException ex) {
            throw new EmailException("Não foi possível enviar email.\nErro: " + ex);
        }
    }
}
